package com.studentSystem.controller;

import com.studentSystem.pojo.Classes;
import com.studentSystem.service.ClassService;
import com.studentSystem.utils.PageBeanUtil;
import com.studentSystem.utils.ResultUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/classes")
@Validated
public class ClassesController {

    @Autowired
    private ClassService classService;


    @GetMapping
    public ResultUtil<List<Class>> list() {
        List<Class> classes = classService.list();
        return ResultUtil.success(classes);
    }

    @GetMapping("/list")
    public ResultUtil<PageBeanUtil<Class>> listAll(
            Integer pageNum,
            Integer pageSize,
            @RequestParam(required = false) @Param("id") Long id,
            @RequestParam(required = false) @Param("grade") String grade,
            @RequestParam(required = false) @Param("advisor") String advisor
    ) {
        PageBeanUtil<Class> pb = classService.listAll(pageNum, pageSize, grade, id, advisor);
        return ResultUtil.success(pb);
    }

    @PostMapping("/add")
    public ResultUtil addClass(@RequestBody Classes classes) {
        System.out.println(classes);
        if (classes.getAdvisor()==null||classes.getGrade()==null||classes.getName()==null) return ResultUtil.error("所有信息不能为空");
        if (classes.getAdvisor().equals("")||classes.getGrade().equals("")||classes.getName().equals("")) return ResultUtil.error("所有信息不能为空");
        classService.addClass(classes);
        return ResultUtil.success();
    }

    @DeleteMapping("/delete")
    public ResultUtil deleteClass(Long id) {
        classService.deleteClass(id);
        return ResultUtil.success();
    }

    @PutMapping("/update")
    public ResultUtil updateClass(@RequestBody Classes classes) {
        classService.updateClass(classes);
        return ResultUtil.success();
    }

    @GetMapping("/all")
    public ResultUtil getAll() {
        List<Classes> classes = classService.all();
        return ResultUtil.success(classes);
    }
}
